# 导包
import unittest
from api.login import LoginAPI
from api.staff import StaffAPI
from lib.dbutil import DBUtil


class TestStaffAPI(unittest.TestCase):
    TOKEN = None
    ID = None

    @classmethod
    def setUpClass(cls) -> None:
        # 实例化类
        cls.login_api = LoginAPI()
        # 实现登录
        response = cls.login_api.login({"mobile": "13800000002", "password": "123456"})
        print(response.json())
        TestStaffAPI.TOKEN = "Bearer " + response.json().get("data")
        print(TestStaffAPI.TOKEN)
        # 清理数据
        DBUtil.exe_sql("delete from bs_user WHERE mobile = '19995052463';")

    @classmethod
    def tearDownClass(cls) -> None:
        DBUtil.exe_sql("delete from bs_user WHERE mobile = '19995052463';")

    def setUp(self) -> None:
        self.staff_api = StaffAPI()

    def tearDown(self) -> None:
        pass

    def test01_get_staff(self):
        response = self.staff_api.get_staff(TestStaffAPI.TOKEN)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get("success"))
        self.assertEqual(10000, response.json().get("code"))
        self.assertIn("操作成功", response.json().get("message"))

    def test02_add_staff(self):
        response = self.staff_api.staff_add(TestStaffAPI.TOKEN,
                                            {"username": "天天帅哥", "mobile": "19995052463",
                                             "workNumber": "152855554573"})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get("success"))
        self.assertEqual(10000, response.json().get("code"))
        self.assertIn("操作成功", response.json().get("message"))
        TestStaffAPI.ID = response.json().get("data").get("id")
        print(TestStaffAPI.ID)

    def test03_staff_inquiry(self):
        response = self.staff_api.staff_inquiry(TestStaffAPI.ID, TestStaffAPI.TOKEN)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get("success"))
        self.assertEqual(10000, response.json().get("code"))
        self.assertIn("操作成功", response.json().get("message"))

    def test04_staff_modify(self):
        response = self.staff_api.staff_modify(TestStaffAPI.ID, TestStaffAPI.TOKEN, {"username": "天天吃dog"})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get("success"))
        self.assertEqual(10000, response.json().get("code"))
        self.assertIn("操作成功", response.json().get("message"))

    def test05_staff_inquiry(self):
        response = self.staff_api.staff_inquiry(TestStaffAPI.ID, TestStaffAPI.TOKEN)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get("success"))
        self.assertEqual(10000, response.json().get("code"))
        self.assertIn("操作成功", response.json().get("message"))

    def test06_staff_delete(self):
        response = self.staff_api.staff_delete(TestStaffAPI.ID, TestStaffAPI.TOKEN)
        print(response.json())
